using System;
using System.Windows.Forms;
using System.IO;
using ArchiveMe.Code;
using ArchiveMe.Forms;

namespace ArchiveMe.Code
{
    static class Program
    {
        const string FileType = "Directory";
        const string KeyName = "Archive Me";
        const string MenuText = "Archive Me :)";

        [STAThread]
        static void Main(string[] args)
        //static void Main()
        {
            //string[] args = new string[1];
            //args[0] = "D:\\Test";

            if (!ProcessCommand(args))
            {
                if (Directory.Exists(args[0]))
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new frmProgress(args[0]));
                }
                else
                {
                    MessageBox.Show(string.Format("{0} folder doesn't exist!", args[0]));
                }
            }
        }

        static bool ProcessCommand(string[] args)
        {
            // show config form
            if (args.Length == 0)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new frmConfig());
                return true;
            }

            // register
            if (string.Compare(args[0], "-r", true) == 0)
            {
                // full path to self, %L is placeholder for selected file
                string menuCommand = string.Format("\"{0}\" \"%L\"", Application.ExecutablePath);

                // register the context menu
                ContextMenuManager.Register(FileType, KeyName, MenuText, menuCommand);

                MessageBox.Show("Archive Me has been registered.", KeyName);

                return true;
            }

            // unregister		
            if (string.Compare(args[0], "-u", true) == 0)
            {
                // unregister the context menu
                ContextMenuManager.Unregister(FileType, KeyName);

                MessageBox.Show("Archive Me has been unregistered!", KeyName);

                return true;
            }

            return false;
        }

    }
}